package com.edu.controller;


import com.edu.domin.dto.CommentDTO;
import com.edu.properties.MyUserDetails;
import com.edu.service.ICommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * <p>
 * 文章评论表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-05-15
 */
@Controller
@RequestMapping("/comment")
@Slf4j
public class CommentController {
    @Autowired
    private ICommentService commentService;

    /**
     * 发表评论
     *
     * @param articleId
     * @param commentDTO
     * @param authentication
     * @return
     */
    @PostMapping("/{articleId}")
    public ResponseEntity<Map<String, Boolean>> save(@PathVariable Long articleId, @RequestBody CommentDTO commentDTO, Authentication authentication) {
        //获取用户身份
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        Long userId = userDetails.getId(); // 安全获取 id
        //保存评论
        commentService.savenew(articleId, userId, commentDTO);
        return ResponseEntity.ok().body(Map.of("success", true));
    }
}
